package com.mcflies.commons.util;

import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;

/**
 * ChineseToPinYin.java
 * 
 * @describe：中文汉字转换为拼音
 * @Copyright(c) 2012-2024 Study,mcfly
 * @author mcfly_jun
 */
public class ChineseToPinYinUtil {
	/**
	 * 将输入的汉字字符串转换成拼音
	 * 
	 * @param src
	 * @param isPloyphone
	 *            :是否查处多音字的所有拼音
	 * @param separatorStr
	 *            :如果查询多音字,用此分隔开
	 * @return
	 */
	public static String chineseToPinYin(String src, boolean isPloyphone, String separatorStr) {
		StringBuffer sb = new StringBuffer();
		String[] pinYinArr = chineseToPinYinWithStringArr(src, isPloyphone, separatorStr);
		for (int i = 0; i < pinYinArr.length; i++) {
			sb.append(pinYinArr[i]);
		}
		return sb.toString();
	}

	/**
	 * @param src
	 * @param isPloyphone
	 * @param separatorStr
	 * @return
	 */
	private static String[] chineseToPinYinWithStringArr(String src, boolean isPloyphone, String separatorStr) {
		if (null == src || "".equals(src)) {
			return null;
		}
		String[] pinYinArr = new String[src.length()];
		for (int i = 0; i < src.length(); i++) {
			char chineseChar = src.charAt(i);
			pinYinArr[i] = chineseToPinYinWithChar(chineseChar, isPloyphone, separatorStr);
		}
		return pinYinArr;
	}

	/**
	 * 将单个字符转换成拼音
	 * 
	 * @param chineseChar
	 * @param isPloyphone
	 * @param separatorStr
	 * @return
	 */
	private static String chineseToPinYinWithChar(char chineseChar, boolean isPloyphone, String separatorStr) {
		HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();
		format.setCaseType(HanyuPinyinCaseType.LOWERCASE);// 设置字母为小写
		format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);// 设置音标方式
		StringBuffer sb = new StringBuffer();
		// 如果是汉字
		if (chineseChar > 128) {
			try {
				String[] convertedArr = PinyinHelper.toHanyuPinyinStringArray(chineseChar, format);
				// 对于无法转换的,我们返回其原本字符
				if (null == convertedArr) {
					sb.append(chineseChar);
				} else {
					if (isPloyphone && null != separatorStr) {
						for (int i = 0; i < convertedArr.length; i++) {
							sb.append(convertedArr[i]);
							if (convertedArr.length != i + 1) {
								sb.append(separatorStr);
							}
						}
					} else {
						sb.append(convertedArr[0]);
					}
				}
			} catch (BadHanyuPinyinOutputFormatCombination e) {
				e.printStackTrace();
			}
		} else {
			sb.append(chineseChar);
		}
		return sb.toString();
	}

	/**
	 * 获取汉字字符串的首字母
	 * 
	 * @param str
	 * @return
	 */
	public static String getChineseToPinYinHeadChar(String str) {
		if (null == str || "".equals(str)) {
			return "";
		}
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < str.length(); i++) {
			char strChar = str.charAt(i);
			sb.append(getChineseHeadChar(strChar));
		}
		return sb.toString();
	}

	/**
	 * 获取字符的首字母
	 * 
	 * @param strChar
	 * @return
	 */
	private static String getChineseHeadChar(char strChar) {
		HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();
		format.setCaseType(HanyuPinyinCaseType.LOWERCASE);// 设置字母为小写
		format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);// 设置音标方式
		StringBuffer sb = new StringBuffer();
		// 如果是汉字
		if (strChar > 128) {
			try {
				String[] convertedArr = PinyinHelper.toHanyuPinyinStringArray(strChar, format);
				// 对于无法转换的,我们返回其原本字符
				if (null == convertedArr) {
					sb.append(strChar);
				} else {
					sb.append(convertedArr[0].charAt(0));
				}
			} catch (BadHanyuPinyinOutputFormatCombination e) {
				e.printStackTrace();
			}
		} else {
			sb.append(strChar);
		}
		return sb.toString();
	}

	@SuppressWarnings("deprecation")
	public static String chineseToPinYinForSimple(String src, String separatorStr) {
		HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();
		format.setCaseType(HanyuPinyinCaseType.LOWERCASE);
		format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
		String convertedStr = "";
		try {
			convertedStr = PinyinHelper.toHanyuPinyinString(src, format, separatorStr);
		} catch (BadHanyuPinyinOutputFormatCombination e) {
			e.printStackTrace();
		}
		return convertedStr;
	}
}
